package com.orange.lswidget.widgets.event.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;

import com.orange.notificationsummary.event.MessageInfo;
import com.orange.notificationsummary.event.pil.defaultimpl.BaseEvent;
import com.orange.util.LogHelper;

public class UnreadSMSDao extends DaoHelper {
	private static final String	TAG	= "UnreadSMSDao";

	public UnreadSMSDao(Context context) {
		super(context);
	}

	public List<MessageInfo> getUnreadMmsInfo() {
		return mNotificationInterfc.getUnreadMmsInfo(mContext);
	}

	public List<MessageInfo> getUnreadSmsInfo() {
		return mNotificationInterfc.getUnreadSmsInfo(mContext);
	}

	public List<Map<String, String>> queryAllUnreadMsgToList() {
		Cursor cursor = mNotificationInterfc.getThreadsInfoCursor(mContext);

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		if (cursor != null) {

			try {
				while (cursor.moveToNext()) {
					Map<String, String> messageMap = new HashMap<String, String>();
					int count = cursor.getColumnCount();
					for (int i = 0; i < count; i++) {
						String columnName = cursor.getColumnName(i);
						String columnValue = cursor.getString(i);

						messageMap.put(columnName, columnValue);
					}

					if (!messageMap.isEmpty()) {
						list.add(messageMap);
					}
				}
			} catch (Exception e) {
				LogHelper.e(TAG, "queryAllUnreadMsgToList. ", e);
			} finally {
				cursor.close();
			}
		}

		return list;
	}

	public List<MessageInfo> isNotShowMMS(List<MessageInfo> mmsList) {
		
		for (int i = 0; i < mmsList.size(); i++) {
			if (!mmsList.get(i).getMmsSubject().trim().equals("")) {
			} else {
				int mid = mmsList.get(i).getMsgId();
				String selection = new String("mid='" + mid + "'");
				
				Cursor cursor = null;
				try {
					cursor = mContext.getContentResolver().query(
							Uri.parse("content://mms/part"), null, selection, null,
							null);

					if (cursor != null) {
						if (cursor.moveToFirst()) {
						} else {
							mmsList.remove(i);
						}
					}
				} catch (Exception e) {
					LogHelper.e(TAG, "isNotShowMMS:", e);
				} finally {
					if (cursor != null) {
						cursor.close();
					}
				}
			}
		}

		return mmsList;
	}

	public List<BaseEvent> getAllUnreadMessageEvent() {
		return mNotificationInterfc.getAllUnreadMessageEvent(mContext);
	}

	public BaseEvent queryUnreadMessageEvent() {
		List<BaseEvent> unreadMsgs = mNotificationInterfc
				.getAllUnreadMessageEvent(mContext);

		BaseEvent unreadMsg = null;
		if (unreadMsgs.size() == 1) {
			unreadMsg = unreadMsgs.get(0);
		}
		return unreadMsg;
	}

	/**
	 * @param pduId
	 *            the column mid of table part refer to it
	 * @return
	 */
	public Map<String, Integer> getAttachmentCountInMMSDetailed(String pduId) {
		return mNotificationInterfc.getAttachmentCountInMMSDetailed(mContext,
				pduId);
	}

	public String getMmsTextContent(String msgId) {
		return mNotificationInterfc.getMmsTextContent(mContext, msgId);
	}

	public Bitmap getMmsPhotobyMmsId(String msgId) {
		return mNotificationInterfc.getMmsPhotobyMmsId(mContext, msgId);
	}

	public String getSmsNumber(String recipient_ids) {
		return mNotificationInterfc.getSmsNumber(mContext, recipient_ids);
	}

	public String getMMSContentLocationUrl(String id) {
		return mNotificationInterfc.getMMSContentLocationUrl(mContext, id);
	}
}
